<?php

namespace App\Models\Payment;

use App\Models\Invoice\Invoice;
use App\Models\ParkingLot\ParkingLot;
use App\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;

class RefundOrder extends Model
{
    use UuidTrait;

    const TABLE = 'refund_orders';

    const STATUS_SUCCESS = Invoice::STATUS_SUCCESS;
    const STATUS_FAILED = Invoice::STATUS_FAILED;
    const STATUS_PENDING = 'PENDING';
    const STATUSES = [
        self::STATUS_SUCCESS => '成功',
        self::STATUS_FAILED => '失败',
        self::STATUS_PENDING => '退款中',
    ];

    const REFUND_TYPE_ORIGINAL_RETURN = 'ORIGINAL_RETURN';

    protected $appends = ['as_refund_status', 'as_refund_amount'];

    public function paymentChannel(): HasOne
    {
        return $this->hasOne(PaymentChannel::class, 'id', 'payment_channel_id');
    }

    public function related(): MorphTo
    {
        return $this->morphTo();
    }

    public function getAsRefundAmountAttribute()
    {
        return floatval($this->refund_amount / 100);
    }

    public function getAsRefundStatusAttribute()
    {
        return \Arr::get(self::STATUSES, $this->refund_status) ?: '';
    }

    public function parkingLot()
    {
        return $this->belongsTo(ParkingLot::class);
    }
}
